package com.zeyi.chat.controller;

import com.alibaba.fastjson.JSON;
import com.zeyi.chat.dto.Message;
import com.zeyi.chat.manager.StageManager;
import com.zeyi.chat.po.User;
import com.zeyi.chat.utils.FXMLUtil;
import com.zeyi.chat.utils.SocketUtil;
import com.zeyi.chat.views.Alert;
import com.zeyi.chat.views.CircleBorder;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

/**
 * @author zeyi
 */
public class LoginController {
    @FXML
    public PasswordField password;

    @FXML
    private TextField email;

    @FXML
    private Button loginBtn;

    @FXML
    void noAccount(ActionEvent event) {
        Stage stage = (Stage) loginBtn.getScene().getWindow();
        Scene scene = FXMLUtil.createScene("/Fxml/Register.fxml");
        new CircleBorder(scene, 5, "#DCDFE6");
        new WindowMove(stage, scene.getRoot());
        stage.setScene(scene);
    }

    @FXML
    void onClose(MouseEvent event) {
        System.exit(0);
    }

    @FXML
    void minimize(MouseEvent mouseEvent) {
        Stage stage = (Stage) loginBtn.getScene().getWindow();
        stage.setIconified(true);
    }

    @FXML
    void login(ActionEvent event) {
        if (!email.getText().equals("") && !password.getText().equals("")) {
            User user = new User(email.getText(), password.getText());
            Message message = new Message("UserHandler/login", "UserHandler/login", JSON.toJSONString(user));
            SocketUtil.send(message);
        } else {
            new Alert("账号和密码不能为空", StageManager.getPrimaryStage());
        }
    }
}
